package com.yxh.lib_base.ext.view.textview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.style.ImageSpan;

/**
 * 2023/2/28 Tu
 * 顶部对齐
 */
public class TopImageSpan extends ImageSpan {
    public TopImageSpan(Context context, int drawableId) {
        super(context, drawableId);
    }

    public TopImageSpan(Context context, Bitmap bitmap) {
        super(context, bitmap);
    }

    public TopImageSpan(Drawable drawable) {
        super(drawable);
    }


    /**
     * update the text line height
     */
    @Override
    public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fontMetricsInt) {  //设置图片块的宽度
        Drawable drawable = getDrawable();
        //注意点，这个rect应该是有效的空间 高度为0，宽度为0 drawable就绘制不出来，在这个地方是用
        //来调整字体高度的，因为要让文本行适应图片高度
        Rect rect = drawable.getBounds();
        if (fontMetricsInt != null) {
            Paint.FontMetricsInt fmPaint = paint.getFontMetricsInt();
            int fontHeight = fmPaint.descent - fmPaint.ascent;
            int drHeight = rect.bottom - rect.top;
            int centerY = fmPaint.ascent + fontHeight / 2;
            fontMetricsInt.ascent = centerY - drHeight / 2;
            fontMetricsInt.top = fontMetricsInt.ascent;
            fontMetricsInt.bottom = centerY + drHeight / 2;
            fontMetricsInt.descent = fontMetricsInt.bottom;
        }
        return rect.right;
    }

    /**
     * see detail message in android.text.TextLine
     *
     * @paramcanvasthe canvas, can be null if not rendering
     * @paramtextthe text to be draw
     * @paramstartthe text start position
     * @paramendthe text end position
     * @paramxthe edge of the replacement closest to the leading margin
     * @paramtopthe top of the line                                                              //文本所在改行的顶部
     * @paramythe baseline                                                                        //文本的基准线
     * @parambottomthe bottom of the line                                                        //文本所在改行的底部 及下行的顶部，xml文件中的设置的行间距会直接影响 bottom到baseline的距离
     * @parampaintthe work paint
     */
    @Override
    public void draw(Canvas canvas, CharSequence text, int start, int end,
                     float x, int top, int y, int bottom, Paint paint) {
//        CharSequence targetText = text.subSequence(start, end);
//        LogUtils.v("VerticalImageSpan", targetText.toString());
        Drawable drawable = getDrawable();
        canvas.save();
        //注意点，这个rect应该是有效的空间 高度为0，宽度为0 drawable就绘制不出来，
        Rect rect = drawable.getBounds();
        Paint.FontMetricsInt fmPaint = paint.getFontMetricsInt();
        int fontHeight = fmPaint.descent - fmPaint.ascent;
        int centerY = y - fontHeight / 2;
        int transY = centerY - (rect.bottom - rect.top) / 2;
        canvas.translate(x, transY);
        drawable.draw(canvas);
        canvas.restore();
    }
}
